package wsf.akkascala

import akka.actor.Actor
import akkascala.ReduceData
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result

import scala.collection.mutable
import scala.collection.mutable.HashMap

/**
 * Created by sjzx on 2016/5/13.
  * Aggregate Actor. receive reduce data merge it to hashmap, then if receive res ,send the data to sender
 */
class AggreateWorker extends Actor{
  var rdata = new HashMap[String, Int]
  override def receive ={
    case ReduceData(data) => doAggregate(data)
    case "res" => println("OK") ;sender ! ReduceData(rdata)
    case _ => unhandled()
  }

  def doAggregate(data:mutable.HashMap[String, Int]) = {
    for(d <- data) {
      if (rdata.contains(d._1)){
        rdata.put(d._1, rdata.getOrElse(d._1, 0) + d._2)
      }
      else
        rdata.put(d._1, d._2)
    }
  }
}
